package com.example.service;

import com.example.entity.User;
import com.example.entity.Account;
import com.example.exception.CustomException;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

import jakarta.annotation.Resource;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Account login(Account account) {
        User user = userMapper.selectByUsername(account.getUsername());
        if (user == null) {
            throw new CustomException("500", "用户不存在");
        }
        // 对输入的密码进行MD5加密后再比较
        String inputPassword = DigestUtils.md5DigestAsHex(account.getPassword().getBytes());
        if (!user.getPassword().equals(inputPassword)) {
            throw new CustomException("500", "密码错误");
        }
        // 将 User 对象的属性复制到新的 Account 对象中
        Account result = new Account();
        result.setId(user.getId().intValue());
        result.setUsername(user.getUsername());
        result.setRole(user.getRole());
        return result;
    }

    public void register(User user) {
        // 检查用户名是否已存在
        User existUser = userMapper.selectByUsername(user.getUsername());
        if (existUser != null) {
            throw new CustomException("500", "用户名已存在");
        }
        // 设置默认角色
        user.setRole("USER");
        // 加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    public void add(User user) {
        // 检查用户名是否已存在
        User existUser = userMapper.selectByUsername(user.getUsername());
        if (existUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        // 加密密码
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    public void update(User user) {
        userMapper.updateById(user);
    }

    public void deleteById(Long id) {
        userMapper.deleteById(id.intValue());
    }

    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            userMapper.deleteById(id.intValue());
        }
    }

    public User selectById(Long id) {
        return userMapper.selectById(id.intValue());
    }

    public List<User> selectAll(User user) {
        return userMapper.selectAll(user);
    }

    public PageInfo<User> selectPage(User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userMapper.selectAll(user);
        return PageInfo.of(list);
    }

    public void resetPassword(Long id, String newPassword) {
        User user = new User();
        user.setId(id);
        user.setPassword(DigestUtils.md5DigestAsHex(newPassword.getBytes()));
        userMapper.updateById(user);
    }

    public User getInfoByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    public void updatePassword(Account account) {
        // 获取用户信息
        User user = userMapper.selectByUsername(account.getUsername());
        if (user == null) {
            throw new CustomException("500", "用户不存在");
        }
        // 验证旧密码
        String oldPassword = DigestUtils.md5DigestAsHex(account.getPassword().getBytes());
        if (!oldPassword.equals(user.getPassword())) {
            throw new CustomException("500", "原密码错误");
        }
        // 更新新密码
        user.setPassword(DigestUtils.md5DigestAsHex(account.getNewPassword().getBytes()));
        userMapper.updateById(user);
    }

    public void updatePassword(User user) {
        // 获取用户信息
        User dbUser = userMapper.selectById(user.getId().intValue());
        if (dbUser == null) {
            throw new CustomException("500", "用户不存在");
        }
        // 验证旧密码
        String oldPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if (!oldPassword.equals(dbUser.getPassword())) {
            throw new CustomException("500", "原密码错误");
        }
        // 更新新密码
        dbUser.setPassword(DigestUtils.md5DigestAsHex(user.getNewPassword().getBytes()));
        userMapper.updateById(dbUser);
    }

    public User getByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    public void updateInfo(User user) {
        // 只更新允许修改的字段：姓名、电话、邮箱
        User existingUser = userMapper.selectByUsername(user.getUsername());
        if (existingUser != null) {
            existingUser.setName(user.getName());
            existingUser.setPhone(user.getPhone());
            existingUser.setEmail(user.getEmail());
            userMapper.updateById(existingUser);
        }
    }
}
